<?php

namespace backend\controllers;

use Yii;
use backend\models\CompanySearch;
use common\models\Company;
use common\models\User;
use common\models\Branch;
use common\models\CategoryCompany;

class CompanyController extends AppController{
    
    /**
     * 
     */
    public function actionIndex(){
        $this->getView()->title = "Quản lý công ty";
        $searchModel = new CompanySearch();
        $dataProvider = $searchModel->search(\Yii::$app->request->get());
        return $this->render('index', array('dataProvider' => $dataProvider, 'searchModel' => $searchModel));
    }
    
    /**
     * 
     */
    public function actionAdd(){
        $this->getView()->title = "Thêm công ty";
        $company = new Company();
        $branch = new Branch();
        $user = new User();
        $user->setScenario("creating");
        
        if (Yii::$app->request->isPost) {
            $company->attributes = Yii::$app->request->post('Company');
            $user->attributes = Yii::$app->request->post('User');
            if($company->validate() && $user->validate()){
                if($company->save()){
                    //create user
                    $user->is_admin = User::TYPE_ADMIN;
                    $user->id_company = $company->id_company;
                    $user->setPassword($user->password_input);
                    $user->save(false);
                    //create branch
                    $branch->name = $company->name;
                    $branch->email = $company->email;
                    $branch->address = $company->address;
                    $branch->phone = $company->phone;
                    $branch->id_company = $company->id_company;
                    $branch->is_main = Branch::IS_MAIN;
                    $branch->save(false);
                    //create category
                    $root = new CategoryCompany();
                    $root->name = 'Chưa xác định nhóm';
                    $root->id_company = $company->id_company;
                    $root->parents = 0;
                    $root->created = date('Y-m-d');
                    $root->saveNode(false);
                    //
                    Yii::$app->session->setFlash('success', "Thông tin cty đã được lưu ");
                    Yii::$app->getResponse()->redirect(['company/index']);
                    Yii::$app->end();
                }
                
            }
        }
        return $this->render('add', array('company' => $company, 'user' => $user));
    }
    
    /**
     * 
     * @param type $id
     */
    public function actionDelete($id = 0){
        $company = Company::find()->where(['id_company' => $id])->one();
        if(!$company){
            Yii::$app->session->setFlash('error', "company don't exists ");
            Yii::$app->getResponse()->redirect(['company/index']);
            Yii::$app->end();
        }
        $company->delete();
        Yii::$app->session->setFlash('success', "the company have deleted! ");
        Yii::$app->getResponse()->redirect(['company/index']);
        Yii::$app->end();
    }
    
    public function actionTest(){
        echo "<pre>";
                var_dump(CategoryCompany::find()->dataFancytree(5));
    }
    
}
